package com.fuxi.cloud.desreport.render.a;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.fuxi.cloud.desreport.model.RenderInfo;
import com.fuxi.cloud.desreport.model.ReportDbInfo;
import com.fuxi.cloud.desreport.render.handler.RowsRenderHandler;
import com.fuxi.cloud.desreport.render.utils.FreeMarkerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
@Component("loopBlockRenderStrategy")
public class a implements RowsRenderHandler {
    private static final Logger a = LoggerFactory.getLogger(a.class);

    public a() {
    }

    @Override
    public boolean support(RenderInfo renderInfo, JSONObject rows) {
        return ObjectUtil.isNotEmpty(renderInfo.getLoopBlockList());
    }

    private String a(JSONObject var1, JSONObject var2, Integer var3, Integer var4) {
        Integer var5 = var4 - var3 + 1;
        StringBuilder var6 = new StringBuilder();
        String var7 = var1.getString("db");
        var6.append("{");
        var6.append(String.format("<#list list as %s>", var7));

        for(int var8 = var3; var8 <= var4; ++var8) {
            var6.append(String.format("\"${(%s_index*%s)+%s+%s}\":", var7, var5, var3, var8 - var3));
            var6.append(var2.get(var8));
            if (var8 < var4) {
                var6.append(",");
            }
        }

        var6.append(String.format("<#if %s_has_next>,</#if>", var7));
        var6.append("</#list>");
        var6.append("}");
        return var6.toString();
    }

    @Override
    public JSONObject render(RenderInfo renderInfo, JSONObject rows) {
        JSONObject var3 = new JSONObject(true);
        Map var4 = renderInfo.getReport().getDataList();
        JSONArray var5 = renderInfo.getLoopBlockList();
        Integer var6 = 0;
        Iterator var7 = var5.iterator();

        JSONObject var9;
        Integer var10;
        ReportDbInfo var13;
        Integer var23;
        do {
            if (!var7.hasNext()) {
                return rows;
            }

            Object var8 = var7.next();
            var9 = (JSONObject)var8;
            var10 = var9.getInteger("sri");
            if (var10 > 0) {
                for(int var11 = 0; var11 < var10; ++var11) {
                    Object var12 = rows.get(var11);
                    if (ObjectUtil.isNotEmpty(var12)) {
                        var3.put(var11 + "", var12);
                    }
                }
            }

            var23 = var9.getInteger("eri");
            String var24 = var9.getString("db");
            var13 = (ReportDbInfo)var4.get(var24);
        } while(!ObjectUtil.isNotEmpty(var13));

        Object var14 = this.getData(var13);
        HashMap var15 = new HashMap(5);
        var15.put("list", var14);
        String var16 = this.a(var9, rows, var10, var23);
        String var17 = FreeMarkerUtils.a(var16, var15);
        JSONObject var18 = JSONObject.parseObject(var17, new Feature[]{Feature.OrderedField});
        Iterator var19 = rows.entrySet().iterator();

        while(var19.hasNext()) {
            Map.Entry var20 = (Map.Entry)var19.next();
            Object var21 = var20.getValue();
            if (var21 instanceof JSONObject) {
                Integer var22 = Integer.parseInt(var20.getKey().toString());
                if (var22 > var23) {
                    var6 = var18.size() + var10;
                    var18.put(var6.toString(), var21);
                }
            }
        }

        var3.putAll(var18);
        return var3;
    }

    @Override
    public Object getData(ReportDbInfo reportDbInfo) {
        return reportDbInfo.getList();
    }
}

